package com.mentaleye.biz.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.StudentPropertyReturnRecordBean;
import com.mentaleye.biz.StudentPropertyLoanRecordBiz;
import com.mentaleye.biz.StudentPropertyReturnRecordBiz;
import com.mentaleye.dal.model.Property;
import com.mentaleye.dal.model.StudentPropertyReturnRecord;
import com.mentaleye.enums.DictionaryEnums;
import com.mentaleye.service.PropertyService;
import com.mentaleye.service.StudentPropertyReturnRecordService;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
import com.mentaleye.util.DateFormatter;
import com.mentaleye.util.ObjectUtil;

@Service
public class StudentPropertyReturnRecordBizImpl implements StudentPropertyReturnRecordBiz {

	private Logger logger = Logger.getLogger(StudentPropertyLoanRecordBiz.class);

	@Autowired
	private PropertyService propertyService;

	@Autowired
	private StudentPropertyReturnRecordService recordService;

	@Override
	public CommonResult insertBean(StudentPropertyReturnRecordBean recordBean) {
		CommonResult result=new CommonResult();
		recordBean.setReturnTime(DateFormatter.parse(recordBean.getReturnTimeString(), "yyyy-MM-dd"));
		
		Property property=null;
		
		try {
			property=this.propertyService.getRecordByKey(recordBean.getPropertyId());
			if(property.getPropertyState()==DictionaryEnums.UNUSE.getCode()){
				result.setResultMsg("该财产已归还！");
			}
			else
			{
				property=new Property();
				property.setPropertyId(recordBean.getPropertyId());
				property.setPropertyState(DictionaryEnums.UNUSE.getCode());
				StudentPropertyReturnRecord record=(StudentPropertyReturnRecord) ObjectUtil.transfer(recordBean, StudentPropertyReturnRecord.class);
				if(this.propertyService.updateRecordBySelective(property)){
					if(this.recordService.insertRecord(record)){
						result.setIsSuccess(true);
						result.setResultMsg("添加财产归还记录成功！");
					}
					else{
						result.setResultMsg("添加财产归还记录失败！");
					}
				}
				else
				{
					result.setResultMsg("添加财产归还记录失败！");
				}	
			}
			
		} catch (Exception e) {
			logger.error("添加财产归还记录异常！", e);
		}
		return result;
	}

	@Override
	public CommonResult updateBean(StudentPropertyReturnRecordBean recordBean) {
		CommonResult result = new CommonResult();
		recordBean.setReturnTime(DateFormatter.parse(recordBean.getReturnTimeString(), "yyyy-MM-dd"));

		try {
			StudentPropertyReturnRecord record = (StudentPropertyReturnRecord) ObjectUtil.transfer(recordBean,
					StudentPropertyReturnRecord.class);
			if (this.recordService.updateRecord(record)) {
				result.setIsSuccess(true);
				result.setResultMsg("修改财产归还记录成功！");
			} else {
				result.setResultMsg("修改财产归还记录失败！");
			}
		} catch (Exception e) {
			logger.error("修改财产归还记录异常！", e);
		}
		return result;
	}

	@Override
	public CommonResult deleteByList(List<Long> idList) {
		boolean isUnUse=false;
		boolean deleteResult = false;
		boolean insertResult = false;
		CommonResult result = new CommonResult();
		List<String> propertyIdList = new ArrayList<String>();

		try {
			List<StudentPropertyReturnRecord> recordList = this.recordService.getListByKeys(idList);
			for (StudentPropertyReturnRecord record : recordList) {
				propertyIdList.add(record.getPropertyId());
			}

			List<Property> propertyList = this.propertyService.getListByKeys(propertyIdList);
			for (Property property : propertyList) {
				if(property.getPropertyState()==DictionaryEnums.UNUSE.getCode()){
					isUnUse=false;
					break;
				}
				else
				{
					property.setPropertyState(DictionaryEnums.IN_USE.getCode());
				}
				
			}

			if(isUnUse){
				result.setResultMsg("财产已归还！");
			}
			else
			{
				deleteResult = this.propertyService.deleteByList(propertyIdList);
				insertResult = this.propertyService.insertByList(propertyList);

				if (deleteResult && insertResult) {
					if (this.recordService.deleteByList(idList)) {
						result.setIsSuccess(true);
						result.setResultMsg("删除财产归还记录成功！");
					} else {
						result.setResultMsg("删除财产归还记录失败！");
					}
				} else {
					result.setResultMsg("删除财产归还记录失败！");
				}
			}
		} catch (Exception e) {
			logger.error("删除财产归还记录异常！", e);
		}
		return result;
	}

	@Override
	public List<StudentPropertyReturnRecord> getList() {
		List<StudentPropertyReturnRecord> recordList = null;
		try {
			recordList = this.recordService.getList();
		} catch (Exception e) {
			logger.error("查询财产归还记录异常！", e);
		}
		return recordList;
	}

	@Override
	public List<StudentPropertyReturnRecordBean> getBeanList() {
		List<StudentPropertyReturnRecordBean> beanList = null;
		try {
			ObjectUtil.transferList(this.recordService.getList(), StudentPropertyReturnRecord.class);
		} catch (Exception e) {
			logger.error("查询财产归还记录异常！", e);
		}
		return beanList;
	}

	@Override
	public void getListByPage(CommonPage page) {
		try {
			page.setPageData(this.recordService.getListByMap(page.pageToMap()));
		} catch (Exception e) {
			logger.error("查询财产归还记录异常！", e);
		}
	}

	@Override
	public Map<String, Object> getMapById(long id) {
		Map<String, Object> resultMap = null;
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("recordId", id);
		try {
			List<Map<String, Object>> mapList = this.recordService.getListByMap(paramMap);
			if (!mapList.isEmpty()) {
				resultMap = mapList.get(0);
			}
		} catch (Exception e) {
			logger.error("查询财产归还记录异常！", e);
		}
		return resultMap;
	}
}
